package cn.edu.nuc.ssm.service.impl;

import cn.edu.nuc.ssm.dao.BbsReplyMapper;
import cn.edu.nuc.ssm.dao.BbsTopicMapper;
import cn.edu.nuc.ssm.dto.BbsTopicAndUsername;
import cn.edu.nuc.ssm.entity.BbsReply;
import cn.edu.nuc.ssm.entity.BbsSection;
import cn.edu.nuc.ssm.entity.BbsTopic;
import cn.edu.nuc.ssm.service.BbsTopicService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Administrator on 2017/4/19.
 */
@Service
public class BbsTopicServiceImpl implements BbsTopicService {

    @Autowired
    private BbsTopicMapper bbsTopicMapper;
    @Autowired
    private BbsReplyMapper bbsReplyMapper;

    @Override
    @Transactional(timeout = 30)
    public int insertBbsTopic(BbsTopic bbsTopic) {

        return bbsTopicMapper.insert(bbsTopic);
    }

    @Override
    public BbsTopic findbyId(Integer id) {

        return bbsTopicMapper.findbyId(id);
    }

    @Override
    public List<BbsTopicAndUsername> findTopicAndUsername(@Param("sid") Integer sid) {
        return bbsTopicMapper.findTopicAndUsername(sid);
    }

    @Override
    public List<BbsTopic> find() {

        return bbsTopicMapper.find();
    }

    @Override
    public void delete(Integer id) {

        List<BbsReply> bbsReplyList = bbsReplyMapper.findbyTid(id);
        if (bbsReplyList.size() > 0 ){

            bbsReplyMapper.deletebyTid(id);
        }

        bbsTopicMapper.deletebyId(id);

    }

    @Override
    public void addViewCount(Integer id) {

        bbsTopicMapper.addCount(id);

    }

    @Override
    @Transactional(timeout = 30)
    public void addbo(Integer id) {

        bbsTopicMapper.updateLocationbyId(1,id);

    }

    @Override
    public List<BbsTopic> findNolocation() {
        return bbsTopicMapper.findbyLocationisnull();
    }

    @Override
    public List<BbsTopic> findbolocation() {
        // 1 表示在轮播图区
        return bbsTopicMapper.findbyLocation(1);
    }

    @Override
    public void removebo(Integer id) {

         bbsTopicMapper.updateLocationbyId(null,id);
    }

    @Override
    @Transactional
    public void updatepicbyid(Integer id, String picname) {

        bbsTopicMapper.updatepicbyId(picname,id);

    }

    @Override
    public void addhot(Integer id) {

        // 2 表示在热门区
        bbsTopicMapper.updateLocationbyId(2,id);
    }

    @Override
    public List<BbsTopic> findhotlocation() {
        // 1 表示在轮播图区
        return bbsTopicMapper.findbyLocation(2);
    }

    @Override
    public void addhit(Integer id) {
        // 3 表示在热门区
        bbsTopicMapper.updateLocationbyId(3,id);
    }

    @Override
    public List<BbsTopic> findhitlocation() {
        // 3 表示在精品
        return bbsTopicMapper.findbyLocation(3);
    }

    @Override
    public List<BbsTopic> findbyUid(Integer id) {

        return bbsTopicMapper.findbyuid(id);
    }

    @Override
    public int updateBbsTopic(BbsTopic bbsTopic) {

        return bbsTopicMapper.updateTitleAndContentAnduidAndSidById(bbsTopic.getTitle(),bbsTopic.getContent(),bbsTopic.getUid(),bbsTopic.getSid(),bbsTopic.getId());
    }
}
